home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1926 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Q: realloc->free?
  5. Date: Wed, 17 Jan 96 18:44:03 GMT
  6. Organization: none
  7. Message-ID: <821904243snz@genesis.demon.co.uk>
  8. References: <4df2ud$706@oxy.rust.net> <4dgic7$qin@unix.sri.com> <dan.821890778@handel>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <dan.821890778@handel> dan@bristol.com "J. Daniel Smith" writes:
  15.  
  16. >While this is indeed correct, ALWAYS having to write this can make for
  17. >ugly code.
  18.  
  19. It depends on how you write it. Also malloc/calloc/realloc calls are likely
  20. to make up a very insignificant portion of the source code.
  21.  
  22. >Let's face it: under normal circumstances, realloc() is not going to
  23. >fail;
  24.  
  25. Very dangerours assumption which is not bourne out in practice.
  26.  
  27. >adding code to deal for the extremely rare case can add
  28. >significantly to the complexity of the code.
  29.  
  30. Adding error checking for heap allocation makes minimal difference to
  31. program complexity. OTOH dealing with rare cases properly makes the task
  32. of debugging much easier.
  33.  
  34. > And if realloc() does
  35. >fail, there are probably a lot bigger things to worry about than
  36. >leaking memory (like the GUI not being able to create a window to tell
  37. >you something has went wrong).
  38.  
  39. You can at the very least exit cleanly at the point of failure. If you fail
  40. in allocation you can try to creewate an error window and exit cleanly
  41. if that fails.
  42.  
  43. >Yes, the above code is more robust than simply
  44. >   a = realloc(a, newsize);
  45. >but this marginal increase in robustness (given the chances of
  46. >realloc() failing in normal operating conditions)
  47.  
  48. You cannot know what the chances are, especially in a multitasking
  49. system. It is foolish to take any chances when avoiding them is so trivial.
  50.  
  51. >evaluated against the added complexity of dealing with the failure.
  52. >Yes, the programmer needs to be aware of the fact that realloc() can
  53. >fail, but in some cases ALWAYS checking the return value of realloc()
  54. >just isn't practical given other constraints.
  55.  
  56. I can't think of any reasonable constraints that would make it impractical.
  57.  
  58. >C++ is able to deal with this a bit more elgantly via exceptions.
  59.  
  60. That's fair enough, the point is that in one way or another errors are
  61. dealt with.
  62.  
  63. -- 
  64. -----------------------------------------
  65. Lawrence Kirby | fred@genesis.demon.co.uk
  66. Wilts, England | 70734.126@compuserve.com
  67. -----------------------------------------
  68.